import {useState} from "react";

// 员工查询今日工作状态

interface TodayAttendanceResponse {
    code: number;
    data: string;
    message: string;
}

interface UseTodayAttendance {
    todayAttendance: () => Promise<void>;
    data: string;
    error: string | null;
}

export function useTodayAttendance(): UseTodayAttendance {
    const [data, setData] = useState('');
    const [error, setError] = useState<string | null>(null);

    const token = localStorage.getItem('token');
    const todayAttendance = async (): Promise<void> => {
        setError(null);
        try {
            if (!token) {
                setError('未提供授权令牌');
                return;
            }
            const response = await fetch('http://localhost:6868/employee/checkTodayAttendance', {
                method: 'GET',
                headers: {
                    'Authorization': token!
                }
            });

            if (!response.ok) {
                throw new Error('异常');
            }

            const responseData: TodayAttendanceResponse = await response.json();
            setData(responseData.data);

        } catch (error) {
            setError((error as Error).message);
        }
    }

    return {todayAttendance, data, error};
}
